using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Olemas.Models;

namespace Olemas.DataModels
{
    public class BookModel
    {
        [DisplayName("")]
        [Required(ErrorMessage = "*")]
        [RegularExpression(@"(\S)+", ErrorMessage = "*")]
        public string Name { get; private set; }

        [DisplayName("")]
        [Required(ErrorMessage = "*")]
        [RegularExpression(@"(\S)+", ErrorMessage = "*")]
        public string Author { get; private set; }

        /// <summary>
        /// Gets the number of books with the given faculty id and return all books if faculty id is "0".
        /// </summary>
        /// <param name="facId">The Id of the faculty of the books displayed.</param>
        public static int GetNumberOfBooks(int facId)
        {
            var db = new OlemasDatabaseEntities();
            var total = (from book in db.Books
                         where (facId == 0 || book.Faculty.Id == facId)
                         select book).Count();
            return total;
        }

        /// <summary>
        /// Gets the books with the given faculty id and orders it based on given string
        /// </summary>
        /// <param name="facId">The Id of the faculty of the books displayed.</param>
        /// <param name="page">The current page</param>
        /// <param name="pageSize">The amount of books to display on one page.</param>
        /// <param name="ordering">What to order the books by.</param>
        public static IQueryable<Book> GetBooks(int facId, int page, int pageSize, string ordering)
        {
            var db = new OlemasDatabaseEntities();
            var books = db.Books.Where(f => facId == 0 || f.Faculty_Id == facId);
            switch (ordering)
            {
                case "NewestFirst":
                    books = books.OrderByDescending(d => d.DateAdded);
                    break;

                case "OldestFirst":
                    books = books.OrderBy(d => d.DateAdded);
                    break;

                case "Author":
                    books = books.OrderBy(x => x.Author);
                    break;

                case "Title":
                    books = books.OrderBy(x => x.Name);
                    break;

                case "Price":
                    books = books.OrderBy(x => x.Price);
                    break;

                default:
                    books = books.OrderByDescending(d => d.DateAdded);
                    break;
            }
            return books.Skip(pageSize * (page - 1)).Take(pageSize);

        }
        /// <summary>
        /// Gets the book with the given id.
        /// </summary>
        /// <param name="id">The Id of the book.</param>
        public static Book GetBookById(int id)
        {
            var db = new OlemasDatabaseEntities();
            var book = db.Books.Find(id);
            return book;
        }

    }
}